package cn.myfirefly.moming.integrate.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.myfirefly.moming.common.entity.MMUser;
import cn.myfirefly.moming.common.model.PositionDto;
import cn.myfirefly.moming.common.model.StaffDto;
import cn.myfirefly.moming.common.model.StaffFilterDto;
import cn.myfirefly.moming.common.model.UserDto;
import cn.myfirefly.moming.common.service.PositionService;
import cn.myfirefly.moming.common.service.StaffService;
import cn.myfirefly.moming.common.service.UserService;
import cn.myfirefly.core.BaseController;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.annotation.BjuiDataGridFilterDtoWrap;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;

@Controller
@RequestMapping("staff")
public class StaffController extends BaseController {

	@Autowired
	private StaffService staffService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private PositionService positionService;
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		return "staff/index";
	}
	
	@RequestMapping("edit.do")
	public String edit(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "staff/edit";
	}
	
	@RequestMapping("add.do")
	public String add(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "staff/edit";
	}
	
	@RequestMapping("save.do")
	@ResponseBody
	public ResultDto save(StaffDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		warpModifyInfo(dto, request);
		String msg = staffService.save(dto);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("保存成功");
	}
	
	@RequestMapping("list.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto<StaffDto> list(StaffFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		PageResultDto<StaffDto> result = staffService.getList(dto);
		return result;
	}
	
	@RequestMapping("getStaff.do")
	@ResponseBody
	public ResultDto getStaff(StaffDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		StaffDto staff = staffService.getStaff(dto.getStaffSn());
		return success(staff);
	}
	
	@RequestMapping("del.do")
	@ResponseBody
	public ResultDto del(StaffDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String msg = staffService.del(dto.getStaffSn(), dto.getStaffNo());
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("删除成功");
	}
	
	
	@RequestMapping("lookupUser.do")
	public String lookupUser(UserDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		PageResultDto<MMUser> resultDto = userService.getList(dto);
		request.setAttribute("resultDto", resultDto);
		request.setAttribute("loginName", dto.getLoginName());
		// 已选的用户
		String userSn = request.getParameter("userSn");
		request.setAttribute("userSn", userSn);
//		UserDto userDto = userService.getUser(Integer.valueOf(userSn));
//		// 已选的用户
//		request.setAttribute("user", userDto);
		return "staff/user-lookup";
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("lookupPosition.do")
	public String lookupPosition(PositionDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		PageResultDto resultDto = positionService.getList(dto);
		request.setAttribute("resultDto", resultDto);
		request.setAttribute("positionName", dto.getPositionName());
		// 已选的职位
		String positionSn = request.getParameter("positionSn");
		request.setAttribute("positionSn", positionSn);
		return "staff/position-lookup";
	}
	
}
